package teemo

import (
	"errors"
	"fmt"
)

// HTTP状态码对应的错误信息
const (
	HttpStatus400Error = "bad request"
	HttpStatus401Error = "unauthorized"
	HttpStatus403Error = "forbidden"
	HttpStatus404Error = "not found"
	HttpStatus405Error = "method not allowed"
	HttpStatus408Error = "request timeout"
	HttpStatus429Error = "too many requests"
	HttpStatus500Error = "internal server error"
	HttpStatus502Error = "bad gateway"
	HttpStatus503Error = "service unavailable"
	HttpStatus504Error = "gateway timeout"
)

// Errors 定义HTTP状态码到错误的映射
type Errors map[int]error

// ErrorMap 全局错误映射实例
var ErrorMap Errors

// init 初始化全局错误映射
func init() {
	ErrorMap = make(map[int]error)
	ErrorMap[400] = errors.New(HttpStatus400Error)
	ErrorMap[401] = errors.New(HttpStatus401Error)
	ErrorMap[403] = errors.New(HttpStatus403Error)
	ErrorMap[404] = errors.New(HttpStatus404Error)
	ErrorMap[405] = errors.New(HttpStatus405Error)
	ErrorMap[408] = errors.New(HttpStatus408Error)
	ErrorMap[429] = errors.New(HttpStatus429Error)
	ErrorMap[500] = errors.New(HttpStatus500Error)
	ErrorMap[502] = errors.New(HttpStatus502Error)
	ErrorMap[503] = errors.New(HttpStatus503Error)
	ErrorMap[504] = errors.New(HttpStatus504Error)
}

// Get 根据HTTP状态码获取对应的错误
// 如果状态码未定义，则返回通用错误信息
func (e Errors) Get(status int) error {
	if err, ok := e[status]; ok {
		return err
	}

	return fmt.Errorf("unexpected http status: %d", status)
}
